package com.wyp.service.impl;


import com.wyp.dao.MSOrderMapper;
import com.wyp.dao.OrderInfoMapper;
import com.wyp.entity.MSOrder;
import com.wyp.entity.OrderInfo;
import com.wyp.entity.User;
import com.wyp.service.MSService;
import com.wyp.utils.redis.RedisService;
import com.wyp.utils.redis.prekey.pre.OrderKey;
import com.wyp.vo.GoodsVO;
import com.wyp.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * @author: Oliver
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderInfoMapper orderInfoMapper;
    @Autowired
    MSOrderMapper MSOrderMapper;

    @Autowired
    MSService MSService;
    @Autowired
    RedisService redisService;

    @Override
    @Transactional
    public OrderInfo createOrder(User user, GoodsVO goods) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(0L);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goods.getId());
        orderInfo.setGoodsName(goods.getGoodsName());
        orderInfo.setGoodsPrice(goods.getMSPrice());
        orderInfo.setOrderChannel(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(user.getId());
        orderInfoMapper.insert(orderInfo);
        MSOrder MSOrder = new MSOrder();
        MSOrder.setGoodsId(goods.getId());
        MSOrder.setOrderId(orderInfo.getId());
        MSOrder.setUserId(user.getId());
        MSOrderMapper.insert(MSOrder);
        //添加缓存
        redisService.set(OrderKey.MS_ORDER_USERID_GOODID, "" + user.getId() + goods.getId(), orderInfo);
        return orderInfo;
    }

    @Override
    public OrderInfo getOrderByUserIdAndGoodsId(User user, GoodsVO goods) {
        //增加缓存处理
        OrderInfo orderInfo = redisService.get(OrderKey.MS_ORDER_USERID_GOODID, "" + user.getId() + goods.getId(), OrderInfo.class);
        return orderInfo;
    }

    @Override
    public OrderInfo getOrderById(long orderId) {
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(orderId);
        return orderInfo;
    }

    @Override
    public List<MSOrder> getAllMSOrdersByGoodsId(long goodsId) {
        return orderInfoMapper.getAllMSOrdersByGoodsId(goodsId);

    }
}
